package com.lyl.goods.mapper;

import com.lyl.goods.pojo.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Liyunlong
 * @Date: 2024/03/01/19:09
 * @Description:
 */
@Mapper
public interface CartMapper {
    @Insert("insert into shopping_cart(product_id, added_time,user_id)VALUES " +
            "(#{productId},now(),#{userId}) ")
    void add(Long productId, Integer userId);
    @Delete("delete from shopping_cart where cart_id=#{cartId}")
    void delete(Long cartId);

    @Select("SELECT * FROM shopping_cart WHERE product_id = #{productId} AND user_id = #{userId}")
    ShoppingCart selectByProductId(@Param("productId") Long productId, @Param("userId") Integer userId);

    @Update("update shopping_cart set quantity =#{quantity} where product_id=#{productId} ")
    void update(Long productId, int quantity);

    @Select("select *from shopping_cart where cart_id=#{cartId}")
    ShoppingCart getByCartId(Long cartId);

    @Update("update shopping_cart set  quantity=#{quantity} where cart_id=#{cartId}")
    void addQuantity(Long cartId, int quantity);

    @Select("select * from shopping_cart where user_id=#{userId}")
    List<ShoppingCart> cartList(Integer userId);

    @Select("SELECT sc.cart_id, p.name AS product_name, p.price AS product_price, sc.quantity, p.price * sc.quantity AS total_price " +
            "FROM shopping_cart sc " +
            "JOIN products p ON sc.product_id = p.id " +
            "WHERE sc.user_id = #{userId}")
    List<Map<String, Object>> getCartItems(Integer userId);

}
